<template>
  <div :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce, addZero } from '@/utils'

export default {
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '240px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null,
      yesterdayData: [],
      todayData: [],
      dayTicks: []
    }
  },
  mounted() {
    /* getChartData().then(res => {
      // this.chartData.visitsData = res.visitsData
      // this.chartData.ipData = res.ipData
      this.initChart()
    })*/
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    setOptions({ visitsData, ipData } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          left: 40,
          top: 10,
          data: ['今日', '昨日']
        },
        xAxis: {
          type: 'category',
          data: this.dayTicks,
          boundaryGap: false,
          nameTextStyle: {
            color: '#999999'
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(0, 0, 0, 0.25)'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: 2,
            color: 'rgba(153, 153, 153, 1)'
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false,
            color: 'rgba(0, 0, 0, 0.25)'
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: 'rgba(153, 153, 153, 1)'
          },
          splitLine: {
            show: false
          }
        },
        series: [{
          name: '昨日', type: 'line',
          symbol: 'circle',
          symbolSize: 4,
          showSymbol: false,
          itemStyle: { color: '#A1A1A1' },
          lineStyle: { color: '#A1A1A1' },
          smooth: {},
          areaStyle: {
            color: new echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                { offset: 0, color: '#EAEAEA' },
                { offset: 1, color: '#FFFFFF' }
              ]
            )
          },
          data: this.yesterdayData
        },
        {
          name: '今日',
          type: 'line',
          symbol: 'circle',
          symbolSize: 4,
          showSymbol: false,
          itemStyle: { color: '#3497DB' },
          lineStyle: { color: '#3497DB' },
          smooth: true,
          data: this.todayData
        }]
      })
    },
    initChart() {
      // 刻度初始化
      for (let i = 0; i < 24; i++) {
        this.dayTicks.push(addZero(i))
        this.todayData.push(Math.floor(Math.random() * 800 + 1))
        this.yesterdayData.push(Math.floor(Math.random() * 1000 + 1))
      }
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    }
  }
}
</script>
